<template>
	<div><el-form
		style="margin-top:20px;"
		    :model="ruleForm"
		    ref="ruleForm"
		    :rules="rules"
		    :label-position="labelPosition"
		    label-width="120px"
		    class="demo-ruleForm ">
		    <el-row class="container">
				<el-page-header @back="goback" content="防疫管理">
				</el-page-header>
				<el-col style="margin-top:20px;">
					<el-form-item label="养殖批号：" prop="spec">
					    <el-input placeholder="请输入" disabled v-model="breed.batch" style="width: 300px">
					      
					    </el-input>
					</el-form-item>
				</el-col>
		        <el-col :span="12" >
		            <el-form-item label="投入药品：" prop="type">
		                <template>
		                    <el-select
		                        v-model="ruleForm.inputsName"
		                        style="width:300px"
		                        placeholder="请选择" @change="getInputsList1">
		                        <el-option v-for="item in inputsList"
		                            :key="item"
		                            :label="item"
		                            :value="item"></el-option>
		                    </el-select>
		                </template>
		            </el-form-item>
					<el-form-item label="投入数量：" prop="spec">
					    <el-input placeholder="请输入" v-model="ruleForm.inputsNumber" style="width: 300px" @change="compareNumber">
					        <template slot="append">
								{{ruleForm.specUnits}}
					            <!-- <el-select
					                v-model="ruleForm.specUnits"
					                placeholder="单位"
					                style="width:80px !important">
					                <el-option
					                    v-for="item in unitsList"
					                    :key="item.id"
					                    :label="item.name"
					                    :value="item.name"
					                ></el-option>
					            </el-select> -->
					        </template>
					    </el-input>
					</el-form-item>
					<el-form-item label="防疫批号：" prop="spec">
					    <el-input placeholder="请输入" v-model="ruleForm.preventBatch" style="width: 300px">
					        <template slot="append">
					            <el-button @click="addBatch">自动生成</el-button>
					        </template>
					    </el-input>
					</el-form-item>
					
				
		        </el-col>
		        <el-col :span="10">
					<el-form-item label="投入品批号：" prop="type">
					    <template>
					        <el-select
					            v-model="ruleForm.inputsBatch"
					            style="width:300px"
					            placeholder="请选择" @change="getInputs">
					            <el-option
					                v-for="item in inputsList1"
					                :key="item.id"
					                :label="item.batch"
					                :value="item"></el-option>
					        </el-select>
					    </template>
					</el-form-item>
					<el-form-item label="防疫日期：">
					    <el-date-picker
					        v-model="ruleForm.preventTime"
					        type="date"
					        value-format="yyyy-MM-dd"
					        placeholder="请选择"
					        style="width:300px"
					    ></el-date-picker>
					</el-form-item>
					<el-form-item label="防疫负责人：" prop="type">
					    <template>
					        <el-select
					            v-model="ruleForm.preventName"
					            style="width:300px"
					            placeholder="请选择">
					            <el-option
					                v-for="item in userList"
					                :key="item.id"
					                :label="item.name"
					                :value="item.name"
					            ></el-option>
					        </el-select>
					    </template>
					</el-form-item>
		        </el-col>
				<el-col>
					<el-form-item label="编号类型：">
                    <el-radio-group v-model="type" @change="gettype">
                        <!-- <el-radio label="耳标" border>耳标</el-radio> -->
                        <el-radio label="RFID" border>RFID</el-radio>	
                        <el-radio label="养殖批号" border>养殖批号</el-radio>
                    </el-radio-group>
					</el-form-item>
					<el-form-item label="RFID：" v-if="type=='RFID'">
						<el-input placeholder="请输入" v-model="param" style="width: 300px">
							<template slot="append">
								<el-button @click="getBreedDetail">导入</el-button>
							</template>
						</el-input>
						<el-button style="margin-left:10px;">批量导入</el-button>
						<!-- <el-button>模板下载</el-button> -->
					</el-form-item>
					<!-- <el-form-item label="养殖批号" v-if="type == '养殖批号'">
						<el-select
							v-model="param"
							@change="addParent1"
							value-key="id"
							style="width:300px"
							placeholder="请选择"
						>
							<el-option
								v-for="item in batchList"
								:key="item.id"
								:label="item.batch"
								:value="item.batch">
							</el-option>
						</el-select>
					</el-form-item> -->
					<el-table
						:data="tableData"
						border
						style="margin-top:20px;"
						ref="multipleTable"
						header-cell-class-name="table-header">
						<el-table-column prop="type" sortable label="编号类型"></el-table-column>
						<el-table-column prop="code" sortable label="编号"></el-table-column>
						<el-table-column prop="sex" sortable label="性别">
							<template slot-scope="scope">
								<el-select v-model="scope.row.sex" placeholder="请选择">
								    <el-option
								      v-for="item in sexlist"
								      :key="item.value"
								      :label="item.value"
								      :value="item.value">
								    </el-option>
								 </el-select>
							</template>
						</el-table-column>
						<el-table-column prop="buyTime" sortable label="最近防疫日期"></el-table-column>
						<el-table-column prop="buyTime" sortable label="最近防疫药品"></el-table-column>
						<el-table-column prop="buyTime" label="操作" width="150" align="center">
							<template slot-scope="scope">
								<el-button
									type="text"
									style="color: red;"
									@click="handleDelete(scope.$index, scope.row)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
				</el-col>
		    </el-row>
		    <el-form-item class="footerFixed" label-width="0px">
		        <el-button type="primary" @click="submitrule('ruleForm')">提交</el-button>
		        <el-button @click="goback">取消</el-button>
		    </el-form-item>
		</el-form>
	</div>
</template>

<script>
import { prevent_save,prevent_findById,inputs_findByAll,breedInputs_findByInputsName,crop_getBreedDetail,breed_findBatchSelect,born_findByBatch } from '@/request/breedApi';
import { user_findByAll,unit_findByList } from '@/request/api';
	export default{
		data(){
			return{
			    breed:{},
				tableData:[],
				radio1:'耳标',
				labelPosition:'right',
				ruleForm:{
				preventBatch:''
				},
				rules:{},
				typeList:[],
				currentPage:1,
			    inputsList:[],
			    inputsList1:[],
			    userList:[],
			    unitsList:[],
				total:0,
				pagesize:10,
                param:'',
                type:'耳标',
				batchList:[],
				sexlist:[
					{value:"雌"},
					{value:"雄"}
				],
			}
		},
		mounted() {
			this.init();
		},
		methods:{
			gettype(){
				this.tableData = []
				this.param = ""
				if(this.type == "养殖批号"){
					born_findByBatch(this.breed.batch +"?isout=1").then((res)=>{
						this.tableData = res.data
						// this.parentCode = "";
					})
				}
			},
			compareNumber(){
				if(this.ruleForm.inputsNumber>this.number){
					this.$notify({
					    title: '提示',
					    message: '投入数量不能大于药品剩余数量',
					    type: 'info',
					    duration:1000
					});
					this.ruleForm.inputsNumber = ""
				}
			},
			// 返回上一页
			goback(){
				this.$router.go(-1)
			},
                addBatch() {
                    var d = new Date();
                    var curr_date = d.getDate();
                    var curr_month = d.getMonth() + 1;
                    var curr_year = d.getFullYear();
                    String(curr_month).length < 2 ? (curr_month = "0" + curr_month): curr_month;
                    String(curr_date).length < 2 ? (curr_date = "0" + curr_date): curr_date;
                    var yyyyMMdd = curr_year + "" + curr_month +""+ curr_date;
                    this.ruleForm.preventBatch = yyyyMMdd + Math.ceil(Math.random()*10000);
                },
			// 页面一进来加载
			init() {
				const _this = this;
				this.id = _this.$route.query.id
				prevent_findById(this.id).then((res) => {
					if (res.code == 200) {
						this.breed = res.data;
					}
				});
				inputs_findByAll("").then((res) => {
					if (res.code == 200) {
						this.inputsList = res.data;
					}
				});
				user_findByAll("").then((res) => {
					if (res.code == 200) {
						this.userList = res.data;
					}
				});
				unit_findByList("").then((res) => {
					if (res.code == 200) {
						this.unitsList = res.data;
					}
				});
				// breed_findBatchSelect("").then((res)=>{
				// 	this.batchList = res
				// })
			},

		    getBreedDetail(){
				var params = {
					type: this.type,
					param: this.param
				};
				crop_getBreedDetail(params).then((res) => {
					if (res.code == 200) {
						this.tableData= this.tableData.concat(res.data);
						this.param = ""
					}
				});
		    },
			addParent1(){
				let data = []
				data.push(this.param)
				var params = {
					type:this.type,
					param:data
				}
				crop_getBreedDetail(params).then((res) => {
					if (res.code == 200) {
						this.tableData= this.tableData.concat(res.data);
					 }
					 this.param = ""
				});
			},
            getInputsList1(){
				var params = {
					inputsName: this.ruleForm.inputsName
				};
				breedInputs_findByInputsName(params).then((res) => {
					if (res.code == 200) {
						this.inputsList1=res.data;
					}
				});
            },
            getInputs(ele){
				this.ruleForm.inputsId=ele.id;
				this.ruleForm.inputsName=ele.inputsName;
				this.ruleForm.inputsBatch=ele.batch;
				this.ruleForm.specUnits = ele.units
				this.number = Number(ele.number) - Number(ele.expend) 
            },
			// 删除
			handleDelete(index,row){
				
			},
			
			handleCurrentChange(val) {
			    this.currentPage = val;
			    
			},
            // 提交
            submitrule(){
                this.$confirm('请仔细核对所填信息，提交后不能修改！', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
					this.ruleForm.breedId=this.breed.id;
					this.ruleForm.seedName=this.breed.seedName;
					this.ruleForm.batch=this.breed.batch;
					this.ruleForm.prevents=this.tableData;
					prevent_save(this.ruleForm).then((res) => {
						if (res.code == 200) {
							this.$router.go(-1)
						}
					});
                })
            }
		}
	}
</script>

<style scoped>
	.el-pagination {
	    text-align: center;
	}
	.avatar-uploader .el-upload {
	    border: 1px dashed #d9d9d9;
	    border-radius: 6px;
	    cursor: pointer;
	    position: relative;
	    width:180px;
	    overflow: hidden;
	  }
	  
	  .avatar-uploader .el-upload:hover {
	    border-color: #409EFF;
	  }
	  .el-upload--text{
	      width:180px;
	  }
	  .avatar-uploader-icon {
	    font-size: 28px;
	    color: #8c939d;
	    width: 178px;
	    height: 178px;
	    line-height: 178px;
	    text-align: center;
	  }
	  .avatar {
	    width: 178px;
	    height: 178px;
	    display: block;
	  }
</style>
